package com.ruoyi.web.controller.cems;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cems.domain.BmsWorkChecklists;
import com.ruoyi.cems.service.IBmsWorkChecklistsService;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cems.domain.BmsEmergencyNotice;
import com.ruoyi.cems.service.IBmsEmergencyNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 应急通知Controller
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@Api(description = "应急响应通知单")
@RestController
@RequestMapping("/cems/emergencyNotice")
public class BmsEmergencyNoticeController extends BaseController
{
    @Autowired
    private IBmsEmergencyNoticeService bmsEmergencyNoticeService;


    /**
     * 查询应急通知列表
     */
    @ApiOperation(value = "应急通知列表")
    @GetMapping("/list")
    public TableDataInfo list(BmsEmergencyNotice bmsEmergencyNotice)
    {
        startPage();
        if(!SecurityUtils.hasRole("manager")){
            //数据权限:普通用户：仅自己
            bmsEmergencyNotice.setManager(false);
            bmsEmergencyNotice.setUserId(getUserId().toString());
        }else {
            bmsEmergencyNotice.setManager(true);
        }
        bmsEmergencyNotice.setUnitCode(getLoginUser().getUser().getUnit());
        List<BmsEmergencyNotice> list = bmsEmergencyNoticeService.selectBmsEmergencyNoticeList(bmsEmergencyNotice);
        return getDataTable(list);
    }

    /**
     * 导出应急通知列表
     */
    @ApiOperation(value = "应急通知列表")
    @Log(title = "应急通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmsEmergencyNotice bmsEmergencyNotice)
    {
        bmsEmergencyNotice.setUnitCode(getLoginUser().getUser().getUnit());
        List<BmsEmergencyNotice> list = bmsEmergencyNoticeService.selectBmsEmergencyNoticeList(bmsEmergencyNotice);
        ExcelUtil<BmsEmergencyNotice> util = new ExcelUtil<BmsEmergencyNotice>(BmsEmergencyNotice.class);
        util.exportExcel(response, list, "应急通知数据");
    }

    /**
     * 获取应急通知详细信息
     */
    @ApiOperation(value = "应急通知详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        BmsEmergencyNotice beNotice = bmsEmergencyNoticeService.selectBmsEmergencyNoticeByID(ID);
        return success(beNotice);
    }

    /**
     * 新增应急通知
     */
    @ApiOperation(value = "新增应急通知")
    @Log(title = "应急通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsEmergencyNotice bmsEmergencyNotice)
    {
        bmsEmergencyNotice.setID(UUID.randomUUID().toString().replace("-", ""));
        bmsEmergencyNotice.setCreatedBy(getUserId().toString());
        bmsEmergencyNotice.setCreatedTime(new Date());
        bmsEmergencyNotice.setUnitCode(getLoginUser().getUser().getUnit());
        return toAjax(bmsEmergencyNoticeService.insertBmsEmergencyNotice(bmsEmergencyNotice));
    }

    /**
     * 修改应急通知
     */
    @ApiOperation(value = "修改应急通知")
    @Log(title = "应急通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsEmergencyNotice bmsEmergencyNotice)
    {
        bmsEmergencyNotice.setUpdatedBy(getUserId().toString());
        bmsEmergencyNotice.setUpdatedTime(new Date());
        return toAjax(bmsEmergencyNoticeService.updateBmsEmergencyNotice(bmsEmergencyNotice));
    }
    /**
     * 启动应急通知
     */
    @ApiOperation(value = "启动应急通知")
    @Log(title = "应急通知", businessType = BusinessType.OTHER)
    @PostMapping("/start")
    public AjaxResult start(@RequestBody BmsEmergencyNotice bmsEmergencyNotice)
    {
        return bmsEmergencyNoticeService.start(bmsEmergencyNotice,getUserId().toString());
    }

    /**
     * 删除应急通知
     */
    @ApiOperation(value = "删除应急通知")
    @Log(title = "应急通知", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(bmsEmergencyNoticeService.deleteBmsEmergencyNoticeByIDs(IDs));
    }
}
